
//搜索用的数据
var DATA = {

    page : 1,

    per_page : 10,

    machine_id : '',

    product_id : '',

    customer_id : '',

    start_at : '',

    end_at : '',

    order_by : '',

    sort : ''

};

//vue的data
var _Global_Data = {

    query_info : DATA,      //查询所需要的数据

    search_info : {},       //用于接受查询数据

    products : [],          //所有可选商品

    machines : [],          //所有可选机器

    subordinates : {},      //本地存储的下级信息

    try_on_info : {},            //货道状态信息

    normal_sort : {

        created_at : false,

        count : false

    }                             //正序

};

var tryonResource = new Resource( API_LIST.TRY_ON , Vue.http );

!function machineInfo() {

    $('.select2_category').css('width',150);

    var vm = new Vue({

        el : '#try_on',

        data : _Global_Data,

        created : function(){

            this.initSearchData()
                .then(this.requestTableData(this.query_info));

        },

        computed : {

        },

        methods : {
            //select2获取value的方法
            getValue : function getSelectValue(e){

                this.query_info[e.target.dataset.name] = e.target.value;

            },
            //清空日期的方法
            resetTime: function resetTime(id) {

                this.$refs[id].value = '';

            },
            //改变页数的时候出发的函数
            pageChange : function pageChange(val){

                this.search_info.page = val;

                this.search_info.per_page = this.query_info.per_page;

                this.requestTableData(this.search_info);

            },
            //改变每页显示数时触发的函数
            sizeChange : function sizeChange(val){

                this.search_info.per_page = val;

                this.query_info.per_page = val;

                this.requestTableData(this.search_info);

            },
            //点击搜索时触发的函数
            confirmSearch : function confirmSearch(){

                this.query_info.start_at = this.$refs.startTime.value;

                this.query_info.end_at = this.$refs.endTime.value;

                var json_object = JSON.stringify(this.query_info);

                this.search_info = JSON.parse(json_object);

                this.requestTableData(this.query_info);

            },
            //排序
            sortFun : function sortFun(e){

                this.query_info.order_by = e.target.dataset.name;

                if( this.normal_sort[e.target.dataset.name] ){

                    this.normal_sort[e.target.dataset.name] = !this.normal_sort[e.target.dataset.name];

                    this.query_info.sort = 'desc';

                }else {

                    this.normal_sort[e.target.dataset.name] = !this.normal_sort[e.target.dataset.name];

                    this.query_info.sort = 'asc';

                }

                this.search_info = JSON.parse(JSON.stringify(this.query_info));

                this.confirmSearch();

            },
            //搜索函数
            requestTableData : function requestTableData( params ){

                var self = this;

                App.blockUI('#tryon_list_form');

                tryonResource.index( params )
                            .then(function(res){

                                if( parseInt(res.data.code) === 0 ){

                                    self.try_on_info = res.data.try_on_logs;

                                    self.try_on_info.per_page = parseInt(self.try_on_info.per_page);

                                }else {

                                    alert(res.data.error);

                                }

                                App.unblockUI('#tryon_list_form');

                            })
                            .catch(function(err){

                                console.log(err);

                                App.unblockUI('#tryon_list_form');

                                alert('网络异常，请重试！');

                            })

            },
            //初始化所需数据
            initSearchData : function initSearchData(){

                var self = this;

                return tryonResource.create()
                                    .then(function(res){

                                        if( parseInt(res.data.code) === 0 ){

                                            self.subordinates = res.data.subordinates;

                                            self.products = res.data.products.glasses;

                                            self.machines = res.data.machines;

                                        }else {

                                            alert(res.data.error);

                                        }

                                    })
                                    .catch(function(err){

                                        console.log(err);

                                        alert('网络异常，请重试！');

                                    })

            }

        }

    })

}();